#!/bin/bash

# Comments to support chkconfig
# chkconfig: 345 85 15
# description: supervisor for plone

# Source function library.
. /etc/rc.d/init.d/functions

prog="supervisor for plone"
supervisor=${buildout:directory}/bin/supervisord
ctlscript=${buildout:directory}/bin/supervisorctl
RETVAL=0

# Go no further if binaries are missing
[ -x "$supervisor" ] || exit 0
[ -x "$ctlscript" ] || exit 0

start() {
  echo -n $"Starting $prog: "
  daemon $supervisor
  RETVAL=$?
  echo
}

stop() {
  echo -n $"Stopping $prog: "
  killproc $supervisor
  echo
}

restart() {
  stop
  start
}

do_status() {
  status $supervisor
  RETVAL=$?
  [ $RETVAL -eq 0 ] && $ctlscript status
}

do_quiet_status() {
  status $supervisor >/dev/null 2>&1
}

case "$1" in
  start)
    do_quiet_status && exit 0
    $1
    ;;
  stop)
    do_quiet_status || exit 0
    $1
    ;;
  restart)
    $1
    ;;
  status)
    do_status
    ;;
  *)
    echo $"Usage: $0 {start|stop|status|restart}"
    exit 1
esac

exit $RETVAL

